package show.me.the.money.util.print;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import show.me.the.money.config.ConfigController;
import show.me.the.money.config.MultiConfigController;


public class PrintController extends MultiConfigController{
	
	private final static Log logger = LogFactory.getLog(PrintController.class);
	private static PrintController pc;
	
	private PrintController(){
		CATALOG = ConfigController.CATA_PRINT;
		fix = ".jrxml";
	}
	
	public static PrintController instance(){
		if(pc == null){
			pc = new PrintController();
		}
		return pc;
	}

	public Print getPrint(String id){
		Element define = defineDoc.getRootElement();
		if(define == null || id == null){
			return null;
		}
		Print print = (Print) cache.get(id);
		if(print == null){
			Document doc = getConfigDoc(id);
			if(doc != null){
				print = new Print();
				print.setDefineDoc(doc);
				print.fixId(id);
				Element el = (Element) define.selectSingleNode("print[@id='"+id+"']");
				if(el != null){
					print.setConfigElement(el);
				}
				cache.put(id, print);
			}else{
				logger.fatal("print id[" + id + "] not found.");
			}
		}
		return print;
	}
	
}
